package com.fankai.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fankai.lease.common.exception.LeaseException;
import com.fankai.lease.common.result.ResultCodeEnum;
import com.fankai.lease.model.entity.*;
import com.fankai.lease.model.enums.ItemType;
import com.fankai.lease.web.admin.mapper.*;
import com.fankai.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fankai.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.fankai.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.fankai.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.fankai.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.fankai.lease.web.admin.vo.fee.FeeValueVo;
import com.fankai.lease.web.admin.vo.graph.GraphVo;
import jakarta.annotation.Resource;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Resource
    private ApartmentFacilityService apartmentFacilityService;
    @Resource
    private ApartmentLabelService apartmentLabelService;
    @Resource
    private ApartmentFeeValueService apartmentFeeValueService;

    @Resource
    private GraphInfoService graphInfoService;

    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    private FacilityInfoMapper facilityInfoMapper;
    @Resource
    private FeeValueMapper feeValueMapper;

    @Resource
    private FeeKeyMapper feeKeyMapper;
    @Resource
    private LabelInfoMapper labelInfoMapper;
    @Resource
    private RoomInfoMapper roomInfoMapper;

    @Override
    @Transactional
    public void saveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {
        Long id = apartmentSubmitVo.getId();
        if (id == null) {
            save(apartmentSubmitVo);
        } else {

            List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
            List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
            List<Long> labelIds = apartmentSubmitVo.getLabelIds();
            List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();


            LambdaQueryWrapper<ApartmentFacility> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityInfoLambdaQueryWrapper
                    .eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(facilityInfoLambdaQueryWrapper);

            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper
                    .eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper
                    .eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper
                    .eq(GraphInfo::getItemType, ItemType.APARTMENT)
                    .eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(graphInfoLambdaQueryWrapper);


            if (!CollectionUtils.isEmpty(graphVoList)) {
                ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
                graphVoList.forEach(graphVo -> {
                    GraphInfo graphInfo = new GraphInfo();
                    graphInfo.setItemType(ItemType.APARTMENT);
                    graphInfo.setItemId(apartmentSubmitVo.getId());
                    graphInfo.setName(graphVo.getName());
                    graphInfo.setUrl(graphVo.getUrl());
                    graphInfoList.add(graphInfo);
                });
                graphInfoService.saveBatch(graphInfoList);
            }


            if (!CollectionUtils.isEmpty(labelIds)) {
                ArrayList<ApartmentLabel> apartmentLabelList = new ArrayList<>();
                labelIds.forEach(labelId -> {
                    ApartmentLabel apartmentLabel = new ApartmentLabel();
                    apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                    apartmentLabel.setLabelId(labelId);
                    apartmentLabelList.add(apartmentLabel);
                });
                apartmentLabelService.saveBatch(apartmentLabelList);
            }


            if (!CollectionUtils.isEmpty(feeValueIds)) {
                ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
                feeValueIds.forEach(feeValueId -> {
                    ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                    apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                    apartmentFeeValue.setFeeValueId(feeValueId);
                    apartmentFeeValueList.add(apartmentFeeValue);
                });
                apartmentFeeValueService.saveBatch(apartmentFeeValueList);
            }


            if (!CollectionUtils.isEmpty(facilityInfoIds)) {
                ArrayList<ApartmentFacility> apartmentFacilityList = new ArrayList<>();
                facilityInfoIds.forEach(facilityInfoId -> {
                    ApartmentFacility apartmentFacility = new ApartmentFacility();
                    apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                    apartmentFacility.setFacilityId(facilityInfoId);
                    apartmentFacilityList.add(apartmentFacility);
                });
                apartmentFacilityService.saveBatch(apartmentFacilityList);
            }
        }
    }

    @Override
    public Page<ApartmentItemVo> pageApartmentItemByQuery(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(page, queryVo);
    }

    @Override
    @Transactional
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id)
                .eq(ApartmentFacility::getIsDeleted, 0);
        List<ApartmentFacility> facilityList = apartmentFacilityService.list(apartmentFacilityLambdaQueryWrapper);
        List<Long> facilityIds = facilityList.stream().map(ApartmentFacility::getFacilityId).toList();

        if (!facilityIds.isEmpty()) {
            LambdaQueryWrapper<FacilityInfo> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityInfoLambdaQueryWrapper.in(FacilityInfo::getId, facilityIds);

            List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectList(facilityInfoLambdaQueryWrapper);

            apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        }


        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id)
                .eq(ApartmentFeeValue::getIsDeleted, 0);
        ;
        List<ApartmentFeeValue> apartmentFeeValueList = apartmentFeeValueService.list(apartmentFeeValueLambdaQueryWrapper);
        List<Long> feeValueIds = apartmentFeeValueList.stream().map(ApartmentFeeValue::getFeeValueId).toList();

        if (!feeValueIds.isEmpty()) {
            LambdaQueryWrapper<FeeValue> feeValueVoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            feeValueVoLambdaQueryWrapper.in(FeeValue::getId, feeValueIds)
                    .eq(FeeValue::getIsDeleted,0);

            List<FeeValue> feeValueList = feeValueMapper.selectList(feeValueVoLambdaQueryWrapper);

            ArrayList<FeeValueVo> feeValueVoList = new ArrayList<>();

            feeValueList.forEach(feeValue -> {
                FeeValueVo feeValueVo = new FeeValueVo();
                BeanUtils.copyProperties(feeValue, feeValueVo);
                LambdaQueryWrapper<FeeKey> feeKeyLambdaQueryWrapper = new LambdaQueryWrapper<>();
                feeKeyLambdaQueryWrapper.eq(FeeKey::getId, feeValue.getFeeKeyId())
                        .eq(FeeKey::getIsDeleted,0);
                FeeKey feeKey = feeKeyMapper.selectOne(feeKeyLambdaQueryWrapper);
                if (feeKey!=null){
                    feeValueVo.setFeeKeyName(feeKey.getName());
                }
                feeValueVoList.add(feeValueVo);
            });
            apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        }


        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id)
                .eq(ApartmentFeeValue::getIsDeleted, 0);
        List<ApartmentLabel> apartmentLabelList = apartmentLabelService.list(apartmentLabelLambdaQueryWrapper);
        List<Long> labelIds = apartmentLabelList.stream().map(ApartmentLabel::getLabelId).toList();

        if (!labelIds.isEmpty()) {
            LambdaQueryWrapper<LabelInfo> labelInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelInfoLambdaQueryWrapper.in(LabelInfo::getId, labelIds);
            List<LabelInfo> labelInfoList = labelInfoMapper.selectList(labelInfoLambdaQueryWrapper);
            apartmentDetailVo.setLabelInfoList(labelInfoList);
        }


        LambdaQueryWrapper<GraphInfo> graphVoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphVoLambdaQueryWrapper.eq(GraphInfo::getItemId, id)
                .eq(GraphInfo::getItemType, ItemType.APARTMENT)
                .eq(GraphInfo::getIsDeleted, 0);

        List<GraphInfo> graphInfoList = graphInfoService.list(graphVoLambdaQueryWrapper);

        ArrayList<GraphVo> graphVoList = new ArrayList<>();
        graphInfoList.forEach(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            graphVoList.add(graphVo);
        });
        apartmentDetailVo.setGraphVoList(graphVoList);
        return apartmentDetailVo;
    }

    @Override
    @Transactional
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
        } else {
            int byId = apartmentInfoMapper.deleteById(id);
            if (byId > 0) {
                LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();

                facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
                apartmentFacilityService.remove(facilityLambdaQueryWrapper);


                LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();

                apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
                apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);


                LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();

                apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
                apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);


                LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();

                graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id)
                        .eq(GraphInfo::getItemType, ItemType.APARTMENT);
                graphInfoService.remove(graphInfoLambdaQueryWrapper);
            } else {
                throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR_NO_EXISTS);
            }
        }
    }
}




